(function(){
	moneyLook.UI = {};
	
	moneyLook.UI.createBaseWindow = function()
	{
		var tabGroup = Ti.UI.createTabGroup();
		
		var homeWindow = moneyLook.UI.createHomeWindow();
		var expenseWindow = moneyLook.UI.createExpenseWindow();
		var repayWindow = moneyLook.UI.createRepayWindow();
		var contactWindow = moneyLook.UI.createContactWindow();
		
		moneyLook.tabHome = Ti.UI.createTab({
			window:homeWindow,
			title:'Home'
		});
		
		moneyLook.tabExpense = Ti.UI.createTab({
			window: expenseWindow,
			title: 'Ce que je dois'
		});
		
		moneyLook.tabRepay = Ti.UI.createTab({
			window: repayWindow,
			title:"Ce qu'on me doit"
		});
		
		moneyLook.tabContact = Ti.UI.createTab({
			window:contactWindow,
			title:'Contact'
		});
		
		tabGroup.addTab(moneyLook.tabHome);
		tabGroup.addTab(moneyLook.tabExpense);
		tabGroup.addTab(moneyLook.tabRepay);
		tabGroup.addTab(moneyLook.tabContact);
		
		return tabGroup;
	};
	
	moneyLook.UI.createHomeWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Home',
			backgroundColor:'#fff'
		});
		
		var topView = Ti.UI.createView({
			top:0,
			left:0,
			layout:'vertical'
		});
		
		var headerLabel = Ti.UI.createLabel({
			text:'Vos payement en retard',
			backgroundColor:'gray',
			height:30,
			width:320
		});
		
		
		var table = Ti.UI.createTableView({
			headerView:headerLabel,
		});
		
		function updateData(){
			var tableData = [];
			var data = moneyLook.DB.getAll();
			for (var i=0; i < data.length; i++) {
				var current = data[i];
				var color = 'white';
				var thirdyDaysBefore = new Date().getTime() - (60*60*1000*24*30);
				if(current.date < thirdyDaysBefore)
				{
					color = 'red';
				}
				var thisRow = Ti.UI.createTableViewRow({
					backgroundColor:color,
					layout:"horizontal",
					height:30
				});
				
				var label = '';
				if(current.to == null || current.to === '')
				{
					label = "Je dois " + current.montant + " a " + current.from; 
				}
				else
				{
					label = current.to + " me doit: " + current.montant; 
				}
				
				var rowLabel = Ti.UI.createLabel({
					text:label,
					left:10,
				});
	
				thisRow.add(rowLabel);
				tableData.push(thisRow);
				
				
			}
			table.setData(tableData);
			
		}
		
		updateData();
		
		Ti.App.addEventListener('databaseUpdated', updateData)
		
		topView.add(table);
		
		win.add(topView);
		
		return win;
	};
	
	moneyLook.UI.createContactWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Par contact',
			layout:'vertical',
			backgroundColor:'#fff'
		});
		
		
		var headerLabel = Ti.UI.createLabel({
			text:"Contact qui me doivent de l'argent",
			backgroundColor:'gray',
			height:30,
			width:320
		});
		
	
		
		var table = Ti.UI.createTableView({
			headerView: headerLabel
		});
		
		function updateData(){
			var data = moneyLook.DB.getRepayContact();
			table.setData(data);
		}
		
		updateData();
		
		Ti.App.addEventListener('databaseUpdated', updateData)
		
		win.add(table);
		
		return win;
	};
	
	moneyLook.UI.createExpenseWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:'Ce que je dois',
			layout:'vertical',
			backgroundColor:'#fff'
		});
		
		win.activity = {
			onCreateOptionsMenu : function(e) {
				var menu = e.menu;
				var m1 = menu.add({ title : 'Nouveau' });
				m1.addEventListener('click', function(e) {
					moneyLook.tabExpense.open(moneyLook.UI.createAddExpenseWindow());
				});
			}
		}
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Nouveau',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				moneyLook.UI.createAddExpenseWindow().open({model:true});
			});
			win.setRightNavButton(b);
		}
		
		var headerLabel = Ti.UI.createLabel({
			text:'Ce que je dois',
			backgroundColor:'gray',
			height:30,
			width:320
		});
		
		var totalText = 'Total:';
		
		var footerLabel = Ti.UI.createLabel({
			text:totalText,
			height:30,
			width:320,
			backgroundColor:'gray'
		});
		
		var table = Ti.UI.createTableView({
			footerView: footerLabel,
			headerView: headerLabel
		});
		
		function updateData(){
			var tableData = [];
			var total = 0;
			var data = moneyLook.DB.getAllExpense();
			for (var i=0; i < data.length; i++) {
				var current = data[i];
				total += current.montant;
				var color = 'white';
				var thirdyDaysBefore = new Date().getTime() - (60*60*1000*24*30);
				if(current.date < thirdyDaysBefore)
				{
					color = 'red';
				}
				var thisRow = Ti.UI.createTableViewRow({
					backgroundColor:color,
					layout:"horizontal",
					height:30
				});
				
				var label = '';
				if(current.to == null || current.to === '')
				{
					label = "Je dois" + current.montant + " a " + current.from; 
				}
				
				var rowLabel = Ti.UI.createLabel({
					text:label,
					left:10,
				});
	
				thisRow.add(rowLabel);
				tableData.push(thisRow);
				
				
			}
			table.setData(tableData);
			totalText = 'Total : ' + total;
			footerLabel.text = totalText;
			table.footerView = footerLabel;
		}
		
		updateData();
		
		Ti.App.addEventListener('databaseUpdated', updateData)
		
		win.add(table);
		
		return win;
	};
	
	moneyLook.UI.createRepayWindow = function()
	{
		var win = Ti.UI.createWindow({
			title:"Ce qu'on me dois",
			layout:'vertical',
			backgroundColor:'#fff'
		});
		
		win.activity = {
			onCreateOptionsMenu : function(e) {
				var menu = e.menu;
				var m1 = menu.add({ title : 'Nouveau' });
				m1.addEventListener('click', function(e) {
					moneyLook.tabRepay.open(moneyLook.UI.createAddRepayWindow());
				});
			}
		}
		
		if (Ti.Platform.osname === 'iphone') {
			var b = Titanium.UI.createButton({
				title:'Nouveau',
				style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN
			});
			b.addEventListener('click',function() {
				moneyLook.UI.createAddRepayWindow().open({model:true});
			});
			win.setRightNavButton(b);
		}
		
		var headerLabel = Ti.UI.createLabel({
			text:"Ce qu'on me dois",
			backgroundColor:'gray',
			height:30,
			width:320
		});
		
		var totalText = 'Total:';
		
		var footerLabel = Ti.UI.createLabel({
			text:totalText,
			height:30,
			width:320,
			backgroundColor:'gray'
		});
		
		var table = Ti.UI.createTableView({
			footerView: footerLabel,
			headerView: headerLabel
		});
		
		function updateData(){
			var tableData = [];
			var total = 0;
			var data = moneyLook.DB.getAllRepay();
			for (var i=0; i < data.length; i++) {
				var current = data[i];
				total += current.montant;
				var color = 'white';
				var thirdyDaysBefore = new Date().getTime() - (60*60*1000*24*30);
				if(current.date < thirdyDaysBefore)
				{
					color = 'red';
				}
				var thisRow = Ti.UI.createTableViewRow({
					backgroundColor:color,
					layout:"horizontal",
					height:30
				});
				
				var label = '';
				if(current.from == null || current.from === '')
				{
					label = "Je dois" + current.montant + " a " + current.to; 
				}
				
				var rowLabel = Ti.UI.createLabel({
					text:label,
					left:10,
				});
	
				thisRow.add(rowLabel);
				tableData.push(thisRow);
				
				
			}
			table.setData(tableData);
			totalText = 'Total : ' + total;
			footerLabel.text = totalText;
			table.footerView = footerLabel;
		}
		
		updateData();
		
		Ti.App.addEventListener('databaseUpdated', updateData)
		
		win.add(table);
		
		return win;
	};
	
	moneyLook.UI.createAddExpenseWindow = function(){
		var win = Ti.UI.createWindow({
			title:'Ajouter une dpense',
			layout:'vertical',
			backgroundColor:'#fff'
		});
		
		var tbName = Ti.UI.createTextField({
			hintText:'note pour la dpense',
			top:10,
			left:10,
			width:200
		});
		
		win.add(tbName);
		
		var tbMontant = Ti.UI.createTextField({
			hintText:'montant',
			top:10,
			left:10,
			width:200,
		});
		
		win.add(tbMontant);
		
		var labelForPicker = Ti.UI.createLabel({
			text:"A qui devez-vous de l'argent?",
			top:10,
			left:10,
		});
		
		win.add(labelForPicker);
		
		var picker = moneyLook.UI.createContactPicker();
		win.add(picker);
		
		var buttonAdd = Ti.UI.createButton({
			title:'Ajouter',
			top:10,
			left:10
		});
		
		buttonAdd.addEventListener('click', function(e){
			moneyLook.DB.addExpense(tbName.value, tbMontant.value , picker.getSelectedRow(0).title);
		});
		
		win.add(buttonAdd);
		
		return win;
	};
	
	moneyLook.UI.createAddRepayWindow = function(){
		var win = Ti.UI.createWindow({
			title:'Ajouter une redevance',
			layout:'vertical',
			backgroundColor:'#fff'
		});
		
		var tbName = Ti.UI.createTextField({
			hintText:'note pour la redevance',
			top:10,
			left:10,
			width:200
		});
		
		win.add(tbName);
		
		var tbMontant = Ti.UI.createTextField({
			hintText:'montant',
			top:10,
			left:10,
			width:200,
		});
		
		win.add(tbMontant);
		
		var labelForPicker = Ti.UI.createLabel({
			text:"Qui vous doit de l'argent ?",
			top:10,
			left:10,
		});
		
		win.add(labelForPicker);
		
		var picker = moneyLook.UI.createContactPicker();
		win.add(picker);
		
		var buttonAdd = Ti.UI.createButton({
			title:'Ajouter',
			top:10,
			left:10
		});
		
		buttonAdd.addEventListener('click', function(e){
			moneyLook.DB.addExpense(tbName.value, tbMontant.value , picker.getSelectedRow(0).title);
		});
		
		win.add(buttonAdd);
		
		return win;
	};
	
	moneyLook.UI.createContactPicker = function()
	{
		var picker = Ti.UI.createPicker({
			top:10,
			left:10,
		});

		//var contactModule = require('modules/contact');
		
		var data = [];
		var result =  Ti.Contacts.getAllPeople(); //contactModule.getContacts();
		for (var i=0; i < result.length; i++) {
			data[i]= Ti.UI.createPickerRow({title:result[i].fullName});
		};
		
		// off by default
		picker.selectionIndicator = true;
		
		picker.add(data);
		
		return picker;
	};
	
})()
